package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.request.RuleInfoRequest;
import com.fin.zw.aiqas.model.request.RuleTableRequest;
import com.fin.zw.aiqas.model.response.RuleInfoResponse;
import com.fin.zw.aiqas.model.response.RuleInfoTableResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.RuleInspectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@RequestMapping("/inspectionRule")
@Api(value = "质检规则", description = "质检规则管理")
public class RuleInspectionController {

    private final static Logger log = LoggerFactory.getLogger(RuleInspectionService.class);

    @Autowired
    private RuleInspectionService ruleInspectionService;

    /**
     * 添加质检规则
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加质检信息", notes = "添加质检信息")
    public ResponseEntity<Void> addRule(@RequestBody RuleInfoRequest request, @ApiIgnore @CurrentUser SessionModel session) {
        ruleInspectionService.addRuleInfo(request, session);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询质检规则列表
     */
    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询质检规则列表信息", notes = "查询质检规则列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<RuleInfoTableResponse>> queryRuleTable( RuleTableRequest request,Pageable pageable) {
        return ResponseEntity.ok().body(ruleInspectionService.queryRuleTable(request,pageable));
    }

    /**
     * 根据id查询单条质检规则
     */
    @GetMapping("/queryRuleById/{id}")
    @ApiOperation(value = "根据ID查询单条质检规则", notes = "根据ID查询单条质检规则")
    public ResponseEntity<RuleInfoResponse> queryRuleById(@PathVariable Long id) {
        return ResponseEntity.ok().body(ruleInspectionService.queryRuleById(id));

    }


    /**
     * 根据ID修改规则列表
     */
    @RequestMapping(value = "/updateRuleById", method = {RequestMethod.PUT, RequestMethod.POST})
    @ApiOperation(value = "根据id修改质检规则信息", notes = "根据id修改质检规则信息")
    public ResponseEntity<String> updateRuleById(@RequestBody RuleInfoRequest request,@ApiIgnore @CurrentUser SessionModel session) {
        ruleInspectionService.updateRuleById(request, session);
        return ResponseEntity.ok().body("修改成功！");
    }


    /**
     * 根据id删除质检信息
     */
    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "根据id删除质检规则信息", notes = "根据id删除质检规则信息")
    public ResponseEntity<String> deleteRuleById(@PathVariable Long id) {
        ruleInspectionService.deleteRuleById(id);
        return ResponseEntity.ok().body("删除成功！");
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "查询质检规则全部信息", notes = "查询质检规则全部信息")
    public ResponseEntity<List<RuleInfoTableResponse>> fetchRuleInfo(RuleTableRequest request) {
        return ResponseEntity.ok().body(ruleInspectionService.fetchRuleInfo(request));
    }

}
